//
// Created by Administrator on 2023/8/2.
//

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>

using namespace std;

class Solution {
public:
    int garbageCollection(vector<string> &garbage, vector<int> &travel) {
        // 找到最远出现垃圾的位置
        int maxIndexM = -1, maxIndexP = -1, maxIndexG = -1;
        int res = 0;
        for (int i = 0; i < garbage.size(); ++i) {
            res += garbage[i].size();
            if (garbage[i].find('M') != std::string::npos) {
                maxIndexM = i;
            }
            if (garbage[i].find('P') != std::string::npos) {
                maxIndexP = i;
            }
            if (garbage[i].find('G') != std::string::npos) {
                maxIndexG = i;
            }
        }
        // 总时间等于捡垃圾的时间加上路程的时间
        if (maxIndexM != -1) res += accumulate(travel.begin(), travel.begin() + maxIndexM, 0);
        if (maxIndexP != -1) res += accumulate(travel.begin(), travel.begin() + maxIndexP, 0);
        if (maxIndexG != -1) res += accumulate(travel.begin(), travel.begin() + maxIndexG, 0);
        return res;
    }


};

int main() {
    Solution s;
    vector<string> garbage{"G", "P", "GP", "GG"};
    vector<int> travel{2, 4, 3};
    cout << s.garbageCollection(garbage, travel) << endl;
    return 0;
}

